
import request from "@/utils/request";
import {BcScheduleModel} from "@/model/BcScheduleModel";
import {PagingModel} from "@/model/PagingModel";
import {DeviceModel} from "@/model/DeviceModel";

export default class ScheduleApi {


    /**
     * 查询配置列表
     *
     * @param searchKey
     * @param pageIndex
     * @param pageSize
     */
    public static listSchedule(deptId: string, searchKey: string, pageIndex: number, pageSize: number): Promise<PagingModel<BcScheduleModel>> {
        return new Promise((resolve, reject) => {
            request({
                url: '/broadcast/schedule/list',
                method: 'get',
                params: {
                    deptId,
                    searchKey,
                    pageNum: pageIndex,
                    pageSize
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    res.data = res.rows.map(item => new BcScheduleModel().deserialize(item))
                    res.total = +res.total
                    res.pageIndex = pageIndex
                    res.pageSize = pageSize
                    resolve(res)
                }
                reject(res);
            })
        })
    }

    /**
     * 创建字典
     * @param schedule
     */
    public static saveSchedule(schedule: BcScheduleModel): Promise<BcScheduleModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/schedule/save`,
                method: 'POST',
                data: schedule
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new BcScheduleModel().deserialize(res.data))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    /**
     * 更新字典
     * @param id
     * @param schedule
     */
    public static updateSchedule(id:string, schedule: BcScheduleModel): Promise<BcScheduleModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/schedule/${id}`,
                method: 'PUT',
                data: schedule
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new BcScheduleModel().deserialize(res.data))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    public static deleteSchedule(id:string): Promise<boolean> {
        return new Promise<boolean>((resolve, reject) => {
            request({
                url: `/broadcast/schedule/${id}`,
                method: 'DELETE',
            }).then((res: any) => {
                if (res) {
                    resolve(res)
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    public static listScheduleDevice(scheduleId: string): Promise<DeviceModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/schedule/${scheduleId}/device/list`,
                method: 'get',
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new DeviceModel().deserialize(item)))
                }
                reject(res);
            })
        })
    }

    public static saveScheduleDeviceRelation(scheduleId: string, deviceIds: string[]): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/schedule/${scheduleId}/device/save`,
                method: 'put',
                data: deviceIds
            }).then((res: any) => {
                if (res) {
                    resolve(res)
                }
                reject(res);
            })
        })
    }
}

